from flask import Blueprint

from packages.common.base import R
from packages.core.auth.decorators import HasPerm
from modules.{{table.moduleName}}.services.{{table.shortTableName}}_service import {{table.tableHumpName}}Service
from packages.common.base import IdForm, IdsForm
from packages.common.base_model import db
from modules.{{table.moduleName}}.validators.{{table.shortTableName}}_validator import {{table.tableHumpName}}Form{% if not table.treeTable %}, {{table.tableHumpName}}PageForm{% endif %}

# 声明一个蓝图
{{table.shortTableName}} = Blueprint('{{table.shortTableName}}', __name__, url_prefix="/{{table.moduleName}}/{{table.tableCamelName}}")

# 声明一个{{table.comment}}业务服务
{{table.shortTableName}}_service = {{table.tableHumpName}}Service(db=db)


@{{table.shortTableName}}.route("/detail", methods=['POST'])
@HasPerm(access="{{table.moduleName}}:{{table.tableCamelName}}:detail", name="通过id获取{{table.comment}}信息")
def {{table.shortTableName}}_detail():
    """
    通过id获取{{table.comment}}信息
    :return:
    """
    form = IdForm()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # 或者直接拿id值 id=form.id.data
    # u = {{table.tableHumpName}}.query.filter_by(id=form.id.data).first()
    # 通过主键查询
    u = {{table.shortTableName}}_service.detail(form)
    if u is not None:
        return R.data(u.to_dict())
    else:
        return R.fail("该记录不存在")

{% if not table.treeTable %}
@{{table.shortTableName}}.route("/page", methods=['POST'])
@HasPerm(access="{{table.moduleName}}:{{table.tableCamelName}}:page", name="分页查询{{table.comment}}列表")
def {{table.shortTableName}}_page():
    """
    分页查询{{table.comment}}列表
    :return:
    """
    form = {{table.tableHumpName}}PageForm()
    form.validate_for_api()
    return R.data({{table.shortTableName}}_service.page(form))
{% endif %}

@{{table.shortTableName}}.route("/save", methods=['POST'])
@HasPerm(access="{{table.moduleName}}:{{table.tableCamelName}}:save", name="添加{{table.comment}}")
def {{table.shortTableName}}_save():
    """
    添加{{table.comment}}
    :return:
    """
    form = {{table.tableHumpName}}Form()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # print(form.data)
    {{table.shortTableName}}_service.save(form)
    return R.success("添加{{table.comment}}成功")


@{{table.shortTableName}}.route("/update", methods=['POST'])
@HasPerm(access="{{table.moduleName}}:{{table.tableCamelName}}:update", name="修改{{table.comment}}")
def {{table.shortTableName}}_update():
    """
    修改{{table.comment}}
    :return:
    """
    form = {{table.tableHumpName}}Form()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # print(form.data)
    {{table.shortTableName}}_service.update(form)
    return R.success("修改{{table.comment}}成功")


@{{table.shortTableName}}.route("/remove", methods=['POST'])
@HasPerm(access="{{table.moduleName}}:{{table.tableCamelName}}:remove", name="删除{{table.comment}}")
def {{table.shortTableName}}_remove():
    """
    删除{{table.comment}}
    :return:
    """
    form = IdsForm()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # print(form.data)
    {{table.shortTableName}}_service.remove(form)
    return R.success("删除{{table.comment}}成功")

{% if table.treeTable %}
@{{table.shortTableName}}.route("/list", methods=['POST'])
@HasPerm(access="{{table.moduleName}}:{{table.tableCamelName}}:list", name="查询{{table.comment}}列表")
def {{table.shortTableName}}_list():
    """
    查询{{table.comment}}列表
    :return:
    """
    return R.data({{table.shortTableName}}_service.list())


@{{table.shortTableName}}.route("/tree", methods=['POST'])
@HasPerm(access="{{table.moduleName}}:{{table.tableCamelName}}:tree", name="查询{{table.comment}}树")
def {{table.shortTableName}}_tree():
    """
    查询{{table.comment}}树
    :return:
    """
    return R.data({{table.shortTableName}}_service.tree())
{% endif %}